package thread;

/**
 * @author lwmzzz
 * @version 1.0
 * @date 2021/1/26 22:58
 * @description 死锁演示
 */
public class DeadLocDemo {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        new Thread() {
            @Override
            public void run() {
                synchronized (sb) {
                    sb.append("a");
                    sb1.append("A");
                    try {
                        sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                synchronized (sb1) {
                    sb.append("b");
                    sb1.append("B");
                    System.out.println(sb);
                    System.out.println(sb1);
                }
            }
        }.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (sb1) {
                    sb.append("c");
                    sb1.append("C");
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                synchronized (sb) {
                    sb.append("d");
                    sb1.append("D");
                    System.out.println(sb);
                    System.out.println(sb1);
                }
            }
        }).start();
    }
}
